<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Presents_mod extends CI_Model
{   
    
    private $_dbTablePresents= 'presents';
    private $_dbTableUsers = 'users';
    private $_dbTableImages = 'images';
    /**
     *  class constructor
     */
    function __construct() 
	{
		// Call the Model constructor
        parent::__construct();
	}
    
    public function getMyPresentsList($id)
    {
        
        $sql = "SELECT {$this->_dbTablePresents}.id,
                       {$this->_dbTablePresents}.from_id,
                       {$this->_dbTablePresents}.to_id,
                       {$this->_dbTablePresents}.present,
                       {$this->_dbTablePresents}.seen,
                       {$this->_dbTablePresents}.timestamp,
                         
                       {$this->_dbTableUsers}.username,
                       {$this->_dbTableUsers}.name,
                       
                       ( SELECT name  FROM {$this->_dbTableImages} WHERE `private`=0 and `user_id`= {$this->_dbTablePresents}.from_id and main=1 LIMIT 1) as image_name
                FROM  {$this->_dbTablePresents}, {$this->_dbTableUsers}
                WHERE {$this->_dbTablePresents}.from_id = {$this->_dbTableUsers}.id
                      AND {$this->_dbTablePresents}.to_id = $id
                      AND {$this->_dbTablePresents}.is_del = 0 /* Nije obrisan */
                ORDER BY {$this->_dbTablePresents}.timestamp DESC;
               ";
        $query = $this->db->query($sql);
        
        //echo $this->db->last_query();
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
        } else { }

        if ($query->num_rows() > 0) {
          $result = $query->result();
          $query->free_result();
        } else {
          $result = NULL;
        }

        return $result;
    }
    
    
    
    /**
     *  AJAX
     */
    
    public function sendPresent($present,$to_id)
    {
        $from = $this->session->userdata('user_id'); // Logovan korisnik salje poklon
        // Prvo proveri da li ima para za poklon
        $query = $this->db->select('coins')
                          ->where('id',$from)
                          ->get($this->_dbTableUsers);
        
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
        } else { }
        
        if ($query->num_rows() == 1) {
          $coins = $query->row()->coins;          
          $query->free_result();          
        } else {
          //$coins = 0;
          return false;
        }        
        $present_cost = $this->config->item('present_cost');
        if( $coins >= $present_cost )
        {
          // Smanji kredit 
          $sql = "UPDATE {$this->_dbTableUsers} SET `coins`=`coins`-{$present_cost} WHERE `id`={$from}; ";
          $query = $this->db->query($sql);
          
          $errors = $this->db->_error_message();
          if (!empty($errors)) {
            throw new Exception("SQL Error: " . $errors);
          } else { }
          
          if ($this->db->affected_rows() != 1) {
              return false;
              //$query->free_result();
          } else { }
        }
        else
        {
          return -2; // Nema para za poke
        }
        
      
        $data = array(
                    'from_id'=>$from,
                    'to_id' => $to_id,
                    'present' => $present
                );
        $this->db->insert($this->_dbTablePresents,$data);
          
        //echo $this->db->last_query();
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
          return false;
        } else { }
               
        
        return 1;
    }
    
    
    public function setPresentIsView($present_id)
    {
      $data = array('seen'=>1);
      
      $query = $this->db->where('id',$present_id)->update($this->_dbTablePresents,$data);
      
      $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
          return false;
        } else { }
               
        
        return true;
    }
    
    
    public function removePresent($present_id)
    {
      $data = array('is_del'=>1);
      
      $query = $this->db->where('id',$present_id)->update($this->_dbTablePresents,$data);
      
      $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
          return false;
        } else { }
               
        
        return true;
    }

    /**
     *  For library
     */
    public function getNumNonViewPresents()
    {
      $id = $this->session->userdata('user_id');
      $query = $this->db->select('from_id')
                        ->from($this->_dbTablePresents)
                        ->where('to_id',$id) // one koje su poslate meni
                        ->where('seen',0) // i koje nisma pregeldao
                        ->get();
      $errors = $this->db->_error_message();
      if (!empty($errors)) {
        throw new Exception("SQL Error: " . $errors);
        return false;
      } else { }
      
      $result = $query->num_rows();
      $query->free_result();      

      return $result;      
    }
    
    
}// end class Presents_mod

/*
      $query = $this->db->select('*')
                        ->from()
                        ->where()
                        ->get();
      //echo $this->db->last_query();
      $errors = $this->db->_error_message();
      if (!empty($errors)) {
        throw new Exception("SQL Error: " . $errors);
      } else { }

      if ($query->num_rows() > 0) {
        $result = $query->result();
        $query->free_result();
      } else {
        $result = NULL;
      }

      return $result;
 */

// end file: application/models/presents_mod.php

?>